﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Net;
using Newtonsoft.Json;
using System.Net.NetworkInformation;


namespace ITU.Yemekhane.App7
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public WebClient client { get; set; }
        public WebClient client2 { get; set; }

        public MainViewModel()
        {
            this.ItemsLeft = new ObservableCollection<ItemViewModel>();
            this.ItemsRight = new ObservableCollection<ItemViewModel>();
            this.client = new WebClient();
            this.client2 = new WebClient();
            this.client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(jsonL_DownloadStringCompleted);
            this.client2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(jsonD_DownloadStringCompleted);
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> ItemsLeft { get; private set; }
        public ObservableCollection<ItemViewModel> ItemsRight { get; private set; }

        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLeftLoaded
        {
            get;
            private set;
        }

        public bool IsDataRightLoaded
        {
            get;
            private set;
        }

        public void LoadLeftData(List<string> FoodData)
        {
            foreach (string item in FoodData)
            {
                this.ItemsLeft.Add(new ItemViewModel() { LineOne = item, LineTwo = "", LineThree = "" });
            }
            this.IsDataLeftLoaded = true;
        }

        public void LoadRightData(List<string> FoodData)
        {
            foreach (string item in FoodData)
            {
                this.ItemsRight.Add(new ItemViewModel() { LineOne = item, LineTwo = "", LineThree = "" });
            }
            this.IsDataRightLoaded = true;
        }

        public void LoadData()
        {
            if (NetworkInterface.GetIsNetworkAvailable()) {
                this.client.DownloadStringAsync(new Uri(getDataUrl(false)));
                this.client2.DownloadStringAsync(new Uri(getDataUrl(true)));
            }
            else
            {
                MessageBoxResult mb = MessageBox.Show("Lütfen bağlantı ayarlarını kontrol edip tekrar deneyiniz.", "Bilgi", MessageBoxButton.OKCancel);
            }
            
        }

        private string getDataUrl(bool isDinner)
        {
            DateTime date = DateTime.Now;//.AddDays(1);
            StringBuilder sb = new StringBuilder();
            sb.Append("http://ituyemekhane.mertsalik.net/data/");
            sb.Append(date.Day.ToString("D2"));
            sb.Append(date.Month.ToString("D2"));
            sb.Append(date.Year);
            if (!isDinner)
            {
                sb.Append("l");
            }
            else
            {
                sb.Append("d");
            }
            sb.Append(".json");
            return sb.ToString();
        }

        void jsonL_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            List<string> foods = new List<string>();
            App.ViewModel.ItemsLeft.Clear();
            if (e.Error != null)
            {
                foods.Add("Haftasonu ve özel günlerde hizmet vermemektedir");
            }
            else
            {
                foods = JsonConvert.DeserializeObject<List<string>>(e.Result);
            }
            LoadLeftData(foods);
        }
        void jsonD_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            List<string> foods = new List<string>();
            App.ViewModel.ItemsRight.Clear();
            if (e.Error != null)
            {
                foods.Add("Haftasonu ve özel günlerde hizmet vermemektedir");
            }
            else
            {
                foods = JsonConvert.DeserializeObject<List<string>>(e.Result);
            }
            LoadRightData(foods);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}